package com.ita.acmsystem.merchant.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

public class JMSUtil {

	private static String MQ_URL = "failover://tcp://10.222.29.195:61616";
	private static String QUEUE_NAME = "kate-queue";

	public static boolean sendMessageToQueue(String str)
			throws FileNotFoundException, IOException, JMSException {
		Properties properties = new Properties();
		properties.load(JMSUtil.class
				.getResourceAsStream("/Jms.properties"));

		// properties.load(new FileInputStream("Jms.properties"));
		MQ_URL = properties.getProperty("MQ_URL",
				"failover://tcp://10.222.29.195:61616");
		QUEUE_NAME = properties.getProperty("QUEUE_NAME", "kate-queue");

		ConnectionFactory cf = new ActiveMQConnectionFactory(MQ_URL);
		Connection conn;
		conn = cf.createConnection();
		Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
		Destination queue = new ActiveMQQueue(QUEUE_NAME);
		MessageProducer producer = session.createProducer(queue);

		conn.start();

		TextMessage tm = session.createTextMessage(str);
		producer.send(tm);

		session.commit();

		producer.close();
		session.close();
		conn.close();
		return true;
	}
}
